package com.alks.function.service.impl.chengKong;

import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.ImageUtils;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.common.utils.excelUtils.ExcelPoiUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.enums.chengkong.DefineEnum;
import com.alks.entity.data.enums.chengkong.NoteEnum;
import com.alks.entity.data.page.PageRecord;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.chengKong.assort.*;
import com.alks.function.data.dto.chengKong.cost.SdStyleProcessNewDto;
import com.alks.function.data.request.chengKong.*;
import com.alks.function.mapper.chengKong.LargeCargoTypeMapper;
import com.alks.function.mapper.chengKong.LargeCargoUsageMapper;
import com.alks.function.mapper.chengKong.PartMapper;
import com.alks.function.mapper.chengKong.RAndDUsageMapper;
import com.alks.function.mapper.pcfactorywork.PcOutListPriceMapper;
import com.alks.function.service.chengKong.LargeCargoUsageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Slf4j
@Service
public class LargeCargoUsageServiceImpl implements LargeCargoUsageService {

    @Resource
    private LargeCargoUsageMapper largeCargoUsageMapper;

    @Resource
    private LargeCargoTypeMapper largeCargoTypeMapper;

    @Resource
    private RAndDUsageMapper rAndDUsageMapper;

    /**
     * 取研发bom
     * @param map
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getDevelopBom(Map<String, String> map) {

        //获取键值
        String styleNo = map.get("styleNo");
        String styleColor = map.get("styleColor");
        String colorCode = map.get("colorCode");
        String overRide = map.get("overRide");

        if (StringUtils.isEmpty(styleNo)) {
            return ResponseInfo.error("请输入型体!");
        }

        if (StringUtils.isEmpty(styleColor)) {
            return ResponseInfo.error("请输入颜色名称!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        String sdStyleNo = largeCargoTypeMapper.getStyleNo(styleNo, companyId);
        if (ObjectUtils.isEmpty(sdStyleNo)) {
            return ResponseInfo.error("大货型体未建档!");
        }

        String sdStyleCfmFlag = largeCargoTypeMapper.getSdStyleCfmFlag(styleNo, companyId);
        if (ObjectUtils.isEmpty(sdStyleCfmFlag)) {
            return ResponseInfo.error("大货型体未审核!");
        }

        String styleUsage = rAndDUsageMapper.getStyleUsage(styleNo, styleColor);
        if (ObjectUtils.isEmpty(styleUsage)) {
            return ResponseInfo.error("研发用量资料未建档!");
        }

        String checkout = rAndDUsageMapper.checkout(styleNo, styleColor);
        if (ObjectUtils.isEmpty(checkout)) {
            return ResponseInfo.error("研发用量资料未审核!");
        }

        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(styleNo, styleColor,companyId);
        if (!ObjectUtils.isEmpty(headNew2)&&"0".equals(overRide)) {
            return ResponseInfo.warn("资料已存在,是否删除重新取数?");
        }
        if ("1".equals(overRide)){
            largeCargoUsageMapper.delBomHead(styleNo,styleColor,companyId);
            largeCargoUsageMapper.delNotesForFiling(styleNo,styleColor,companyId);
            largeCargoUsageMapper.delDevelopBomUsage(styleNo,styleColor,companyId);
            largeCargoUsageMapper.delBomUsage(styleNo,styleColor,companyId);
        }

        String userName = UserIdThread.get().getUserName();
        YfBomHeadNew2 yfBomHeadNew = largeCargoUsageMapper.getYfBomHeadNew(styleNo, styleColor);
        SdBomHeadNew2 sdBomHeadNew2 = new SdBomHeadNew2();
        sdBomHeadNew2.setCompanyId(companyId);
        sdBomHeadNew2.setSysUser(userName);
        sdBomHeadNew2.setSysDate(LocalDateTime.now());
        sdBomHeadNew2.setStyleNo(yfBomHeadNew.getStyleNo());
        sdBomHeadNew2.setStyleColor(yfBomHeadNew.getStyleColor());
        sdBomHeadNew2.setBaseSize(yfBomHeadNew.getBaseSize());
        sdBomHeadNew2.setColorCode(yfBomHeadNew.getColorCode());

        //保存到SD_BOM_HEAD_NEW2
        largeCargoUsageMapper.addSdBomHeadNew2(sdBomHeadNew2);

        List<YfBomPartNew2> styleColorMaterialList = rAndDUsageMapper.getStyleColorMaterialList(styleNo, styleColor);
        if (!ObjectUtils.isEmpty(styleColorMaterialList)) {
            List<SdBomPartNew2> sdBomPartNew2s = styleColorMaterialList.stream().map(s -> {
                SdBomPartNew2 sdBomPartNew2 = new SdBomPartNew2();
                BeanUtil.copyProperties(s, sdBomPartNew2);
                String typeNo = PartServiceImpl.getTypeNo(s.getMtlNo2());
                sdBomPartNew2.setMtlTypeNo(typeNo);
                sdBomPartNew2.setCompanyId(companyId);
                sdBomPartNew2.setSegment(Integer.parseInt(s.getSegment()));
                return sdBomPartNew2;
            }).collect(Collectors.toList());

            //保存到SD_BOM_PART_NEW2
            largeCargoUsageMapper.addSdBomPartNew2List(sdBomPartNew2s);

            //贴合子材料
            for (YfBomPartNew2 yfBomPartNew2 : styleColorMaterialList) {
                List<YfBomFitSub> yfBomFitSub = largeCargoUsageMapper.getYfBomFitSub(yfBomPartNew2.getMtlNo2());
                if (!ObjectUtils.isEmpty(yfBomFitSub)) {
                    List<SdBomFitSub> sdBomFitSubs = yfBomFitSub.stream().map(s -> {
                        SdBomFitSub sdBomFitSub = new SdBomFitSub();
                        BeanUtil.copyProperties(s, sdBomFitSub);
                        sdBomFitSub.setCompanyId(companyId);
                        sdBomFitSub.setSysUser(userName);
                        return sdBomFitSub;
                    }).collect(Collectors.toList());
                    largeCargoUsageMapper.delSdBomFitSub(yfBomPartNew2.getMtlNo2(), companyId);
                    //添加
                    largeCargoUsageMapper.addSdBomFitSubList(sdBomFitSubs);
                }
            }

            //处理注意事项
            List<YfBomMemoNew2Dto> precautionsForFiling = rAndDUsageMapper.getPrecautionsForFiling(styleNo, styleColor);
            if (!ObjectUtils.isEmpty(precautionsForFiling)) {
                List<SdBomMemoNew2> sdBomMemoNew2s = precautionsForFiling.stream().map(s -> {
                    SdBomMemoNew2 sdBomMemoNew2 = new SdBomMemoNew2();
                    BeanUtil.copyProperties(s, sdBomMemoNew2);
                    sdBomMemoNew2.setCompanyId(companyId);
                    sdBomMemoNew2.setStyleNo(styleNo);
                    sdBomMemoNew2.setStyleColor(styleColor);
                    return sdBomMemoNew2;
                }).collect(Collectors.toList());
                largeCargoUsageMapper.addSdBomMemoNew2(sdBomMemoNew2s);
            }

            //全码用量资料
            for (YfBomPartNew2 yfBomPartNew2 : styleColorMaterialList) {
                FullCodeRenewGetRequest request = new FullCodeRenewGetRequest();
                request.setStyleNo(yfBomPartNew2.getStyleNo());
                request.setStyleColor(yfBomPartNew2.getStyleColor());
                request.setSegment(BigDecimal.valueOf(Long.parseLong(yfBomPartNew2.getSegment())));
                request.setPartName(yfBomPartNew2.getPartName());
                List<YfBomUsageNew2> usageNew2s = rAndDUsageMapper.getFullCodeDetailRenewList(request);
                if (!ObjectUtils.isEmpty(usageNew2s)) {
                    List<SdBomUsageNew2> usageNew2List = usageNew2s.stream().map(s -> {
                        SdBomUsageNew2 sdBomUsageNew2 = new SdBomUsageNew2();
                        BeanUtil.copyProperties(s, sdBomUsageNew2);
                        sdBomUsageNew2.setCompanyId(companyId);
                        return sdBomUsageNew2;
                    }).collect(Collectors.toList());
                    largeCargoUsageMapper.addSdBomUsageNew2List(usageNew2List);
                }
            }
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 查询
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getDevelopBomList(UsageDetailsQueryChangeRequest request) {
        PageRecord<String> list = new PageRecord<>();
        //型体查询
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<String> styleQuery = largeCargoUsageMapper.getDevelopBomList(request);
        PageInfo list2 = new PageInfo(styleQuery);
        list.setList(styleQuery);
        list.setTotal(list2.getTotal());
        if (ObjectUtils.isEmpty(styleQuery)){
            return ResponseInfo.error("无该型体颜色的BOM资料!");
        }
        return ResponseInfo.ok(list);
    }

    /**
     * 型体图片查询
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getDevelopBomPhotoList(UsageDetailsQueryChangeRequest request) {
        PageRecord<StyleToPhotoDto> list = new PageRecord<>();
        //型体颜色图片查询
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<StyleToPhotoDto> styleToColorQuery = largeCargoUsageMapper.getDevelopBomPhotoList(request);
        PageInfo pageInfo=new PageInfo(styleToColorQuery);
        list.setList(styleToColorQuery);
        list.setTotal(pageInfo.getTotal());

        return ResponseInfo.ok(list);
    }

    /**
     * 用量明细资料
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getDevelopBomUsageList(StyleNoToColorRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo())||StringUtils.isEmpty(request.getStyleColor())){
            return ResponseInfo.error("型体与颜色不能为空!");
        }
        List<Map<String, String>> typeNoList = largeCargoUsageMapper.getTypeNoList();
        String companyId = UserIdThread.get().getCompanyId();
        List<YfBomPartNew2Dto> developBomUsageList = largeCargoUsageMapper.getDevelopBomUsageList(request.getStyleNo(),request.getStyleColor(),companyId);
        for (YfBomPartNew2Dto yfBomPartNew2Dto : developBomUsageList) {
            if (!ObjectUtils.isEmpty(yfBomPartNew2Dto.getUsageType())) {
                switch (yfBomPartNew2Dto.getUsageType()) {
                    case "B":
                        yfBomPartNew2Dto.setUsageType(DefineEnum.BASIC_CODE.getValue());
                        break;
                    case "M":
                        yfBomPartNew2Dto.setUsageType(DefineEnum.MIDDLE_CODE.getValue());
                        break;
                    case "F":
                        yfBomPartNew2Dto.setUsageType(DefineEnum.FULL_CODE.getValue());
                        break;
                    default:
                        yfBomPartNew2Dto.setUsageType(null);
                }
            }
            if (!ObjectUtils.isEmpty(typeNoList)){
                for (Map<String, String> map : typeNoList) {
                    if (ObjectUtils.isEmpty(yfBomPartNew2Dto.getTypeName())){
                        String typeNo = partService.getTypeNo(yfBomPartNew2Dto.getMtlNo());
                        if (StringUtils.isNotEmpty(typeNo) && Objects.equals(map.get("typeNo"), typeNo)) {
                            yfBomPartNew2Dto.setTypeName(map.get("typeName"));
                        }
                    }
                }
            }
        }
        return ResponseInfo.ok(developBomUsageList);
    }

    /**
     * 0检核/1取消检核
     * @param request
     * @param choose
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getCheckOut(StyleNoToColorRequest request, Integer choose) {
        if (StringUtils.isEmpty(request.getStyleNo())||StringUtils.isEmpty(request.getStyleColor())){
            return ResponseInfo.error("型体与颜色不能为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(request.getStyleNo(), request.getStyleColor(),companyId);
        switch (choose) {
            case 0:
                if (headNew2 != null && "Y".equals(headNew2.getCfmFlag())) {
                    return ResponseInfo.error("型体资料已检核!");
                }
                List<YfBomPartNew2Dto> usageList = largeCargoUsageMapper.getDevelopBomUsageList(request.getStyleNo(), request.getStyleColor(), companyId);
                //判断计算方式和用量是否为空
                int usageTypeCount=0;
                int usageCount=0;
                for (YfBomPartNew2Dto dto : usageList) {
                    if (com.alks.common.utils.beanutils.ObjectUtils.isEmpty(dto.getUsageType())){
                        usageTypeCount++;
                    }
                    if (com.alks.common.utils.beanutils.ObjectUtils.isEmpty(dto.getUsage())){
                        usageCount++;
                    }
                }
                if (usageTypeCount>0){
                    return ResponseInfo.error("型体:"+request.getStyleNo() + ",颜色:"+request.getStyleColor()+"BOM有"+usageTypeCount+"个计算方式为空的记录!");
                }
                if (usageCount>0){
                    return ResponseInfo.error("型体:"+request.getStyleNo() + ",颜色:"+request.getStyleColor() + "BOM有"+usageCount+"个用量为空的记录!");
                }
                String userName = UserIdThread.get().getUserName();
                String cfmFlag = DefineEnum.CHECKOUT_FLAG.getValue();
                largeCargoUsageMapper.updateSdBomCheckOut(request.getStyleNo(),request.getStyleColor(), userName,companyId, cfmFlag, LocalDateTime.now());

                //写入部位更新日志
                List<SdStyleProcessNewDto> processNewList = pcOutListPriceMapper.getSdStyleProcessNewList(request.getStyleNo(), request.getStyleColor(),companyId);
                List<SdBomPartNew2> new2List = largeCargoUsageMapper.getSdBomPartNew2List(request.getStyleNo(), request.getStyleColor(), companyId);
                if (!ObjectUtils.isEmpty(processNewList)){

                    Map<String, String> mtlNameMap = processNewList.stream()
                            .collect(Collectors.toMap(SdStyleProcessNewDto::getPartName, SdStyleProcessNewDto::getMtlName));
                    Set<String> partNamesSet = new HashSet<>();
                    List<SdBomPartNew2> list = new2List.stream().filter(new Predicate<SdBomPartNew2>() {
                        @Override
                        public boolean test(SdBomPartNew2 sdBomPartNew2) {
                            if (!partNamesSet.contains(sdBomPartNew2.getPartName())) {
                                partNamesSet.add(sdBomPartNew2.getPartName());
                                return true;
                            }
                            return false;
                        }
                    }).collect(Collectors.toList());
                    list.sort(Comparator.comparing(SdBomPartNew2::getSeqNo));

                    List<PcDiffPart> pcDiffParts=new ArrayList<>();
                    for (SdBomPartNew2 t : list) {
                        String partName = t.getPartName();
                        String mtlName = t.getMtlName();
                        if (!mtlNameMap.containsKey(partName) ||!mtlName.equals(mtlNameMap.get(partName))) {
                            PcDiffPart diffPart = new PcDiffPart();
                            diffPart.setStyle(t.getStyleNo());
                            diffPart.setStyleColor(t.getStyleColor());
                            diffPart.setCompanyId(companyId);
                            diffPart.setPartName(t.getPartName());
                            diffPart.setMtlName(t.getMtlName());
                            diffPart.setMtlUnit(t.getMtlUnit());
                            diffPart.setMtlSpec(t.getMtlSpec());
                            diffPart.setSysUser(UserIdThread.get().getLoginName());
                            diffPart.setSysDate(LocalDateTime.now());
                            diffPart.setSeqNo(BigDecimal.valueOf(t.getSeqNo()));
                            pcDiffParts.add(diffPart);
                        }
                    }
                    if (!ObjectUtils.isEmpty(pcDiffParts)){
                        largeCargoUsageMapper.delPcDiffPart(companyId,request.getStyleNo(),request.getStyleColor());
                        largeCargoUsageMapper.addPcDiffPartList(pcDiffParts);
                    }
                }
                break;
            case 1:
                if (headNew2 == null || !"Y".equals(headNew2.getCfmFlag())) {
                    return ResponseInfo.error("该型体颜色未检核,不能取消检核!!");
                }
                largeCargoUsageMapper.updateSdBomCheckOut(request.getStyleNo(),request.getStyleColor(), null,companyId, null, null);
                break;
            default:
                return ResponseInfo.error("参数错误!");
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 注意事项建档-插入事项名称
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getNotesForFiling(StyleNoToColorRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo())||StringUtils.isEmpty(request.getStyleColor())){
            return ResponseInfo.error("型体与颜色不能为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();

        List<YfBomMemoNew2Dto> precautionsForFiling = rAndDUsageMapper.getPrecautionsForFiling(request.getStyleNo(), request.getStyleColor());
        if (!ObjectUtils.isEmpty(precautionsForFiling)){
            List<YfBomMemoNew2Dto> list = largeCargoUsageMapper.getNotesForFilingList(request.getStyleNo(), request.getStyleColor(),companyId);
            List<SdBomMemoNew2> sdBomMemoNew2s = precautionsForFiling.stream().map(s -> {
                SdBomMemoNew2 sdBomMemoNew2 = new SdBomMemoNew2();
                BeanUtil.copyProperties(s, sdBomMemoNew2);
                sdBomMemoNew2.setCompanyId(companyId);
                sdBomMemoNew2.setStyleNo(request.getStyleNo());
                sdBomMemoNew2.setStyleColor(request.getStyleColor());
                return sdBomMemoNew2;
            }).collect(Collectors.toList());
            if (ObjectUtils.isEmpty(list)){
                largeCargoUsageMapper.addSdBomMemoNew2(sdBomMemoNew2s);
            }else {
                largeCargoUsageMapper.delNotesForFiling(request.getStyleNo(),request.getStyleColor(), companyId);
                largeCargoUsageMapper.addSdBomMemoNew2(sdBomMemoNew2s);
            }
        }else {
            List<Map<String, String>> list = NoteEnum.noteList();
            List<SdBomMemoNew2> new2s = list.stream().map(s -> {
                SdBomMemoNew2 sdBomMemoNew2 = new SdBomMemoNew2();
                sdBomMemoNew2.setCompanyId(companyId);
                sdBomMemoNew2.setStyleNo(request.getStyleNo());
                sdBomMemoNew2.setStyleColor(request.getStyleColor());
                sdBomMemoNew2.setSeqNo(s.get("seqNo"));
                sdBomMemoNew2.setSeqName(s.get("seqName"));
                return sdBomMemoNew2;
            }).collect(Collectors.toList());
            largeCargoUsageMapper.addSdBomMemoNew2(new2s);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 注意建档事项列表
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getNotesForFilingList(StyleNoToColorRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo())||StringUtils.isEmpty(request.getStyleColor())){
            return ResponseInfo.error("型体与颜色不能为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        List<YfBomMemoNew2Dto> list = largeCargoUsageMapper.getNotesForFilingList(request.getStyleNo(), request.getStyleColor(), companyId);
        return ResponseInfo.ok(list);
    }

    /**
     * 注意事项建档保存
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getNotesForFilingSave(PrecautionsForFilingUpdateRequest request) {
        if (ObjectUtils.isEmpty(request.getStyleNo()) || ObjectUtils.isEmpty(request.getStyleColor())) {
            log.error("型体:{},颜色:{},不能为空!", request.getStyleNo(), request.getStyleColor());
            return ResponseInfo.error("型体与颜色不能为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(request.getStyleNo(), request.getStyleColor(),companyId);
        if (headNew2 != null && "Y".equals(headNew2.getCfmFlag())) {
            return ResponseInfo.error("型体资料已检核!");
        }
        if (!ObjectUtils.isEmpty(request.getList())){
            List<SdBomMemoNew2> list = request.getList().stream().map(s -> {
                SdBomMemoNew2 sdBomMemoNew2 = new SdBomMemoNew2();
                BeanUtil.copyProperties(s, sdBomMemoNew2);
                sdBomMemoNew2.setCompanyId(companyId);
                sdBomMemoNew2.setStyleNo(request.getStyleNo());
                sdBomMemoNew2.setStyleColor(request.getStyleColor());
                return sdBomMemoNew2;
            }).collect(Collectors.toList());
            largeCargoUsageMapper.delNotesForFiling(request.getStyleNo(),request.getStyleColor(), companyId);
            largeCargoUsageMapper.addSdBomMemoNew2(list);
        }else {
            largeCargoUsageMapper.delNotesForFiling(request.getStyleNo(),request.getStyleColor(), companyId);
        }
        return ResponseInfo.ok("操作成功!");
    }

    @Resource
    private PartMapper partMapper;

    @Autowired
    private PcOutListPriceMapper pcOutListPriceMapper;
    /**
     * 保存
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getDevelopBomUsageSave(StyleColorMaterialAddRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo()) || StringUtils.isEmpty(request.getStyleColor())) {
            log.error("型体:{},颜色:{},不能为空!", request.getStyleNo(), request.getStyleColor());
            return ResponseInfo.error("型体或型体颜色不能为空!");
        }

        String companyId = UserIdThread.get().getCompanyId();
        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(request.getStyleNo(), request.getStyleColor(),companyId);
        if (headNew2 != null && "Y".equals(headNew2.getCfmFlag())) {
            return ResponseInfo.error("型体资料已检核!");
        }
        if (!ObjectUtils.isEmpty(request.getStyleColorMaterialRequests())) {
            //判断部位是否存在
            for (StyleColorMaterialRequest s : request.getStyleColorMaterialRequests()) {
                if (StringUtils.isEmpty(s.getPartName())) {
                    log.error("请输入部位!");
                    return ResponseInfo.error("请输入部位!");
                }

                String partName = partMapper.getPartName(s.getPartName());
                if (StringUtils.isEmpty(partName)) {
                    log.error("部位:{},输入错误!", s.getPartName());
                    return ResponseInfo.error("部位输入错误!");
                }
            }

            //处理数据
            List<SdBomPartNew2> list = request.getStyleColorMaterialRequests().stream().map(s -> {

                SdBomPartNew2 bomPartNew2 = new SdBomPartNew2();
                BeanUtil.copyProperties(s, bomPartNew2);
                bomPartNew2.setCompanyId(companyId);
                bomPartNew2.setStyleNo(request.getStyleNo());
                bomPartNew2.setMtlNo2(s.getMtlNo());
                bomPartNew2.setStyleColor(request.getStyleColor());
                if (s.getMtlTypeNo() == null || s.getMtlTypeNo().isEmpty()) {
                    bomPartNew2.setMtlTypeNo(PartServiceImpl.getTypeNo(s.getMtlNo()));
                }

                //判断计算方式
                if (!StringUtils.isEmpty(s.getUsageType())) {
                    switch (s.getUsageType()) {
                        case "全码":
                            bomPartNew2.setUsageType("F");
                            break;
                        case "中间码":
                            bomPartNew2.setUsageType("M");
                            break;
                        case "基本码":
                            bomPartNew2.setUsageType("B");
                            break;
                        default:
                            bomPartNew2.setUsageType(null);
                    }
                }

                //判断是否捡料
                if (!StringUtils.isEmpty(s.getNoLotUsage())) {
                    switch (s.getNoLotUsage()) {
                        case "0":
                            bomPartNew2.setNoLotUsage("Y");
                            break;
                        default:
                            bomPartNew2.setNoLotUsage(null);
                    }
                }
                return bomPartNew2;
            }).collect(Collectors.toList());

            //重排序号
            //生成一个整数流
            List<SdBomPartNew2> updatedList = IntStream.range(0, list.size())
                    //映射对象
                    .mapToObj(i -> {
                        SdBomPartNew2 bomPartNew2 = new SdBomPartNew2();
                        BeanUtil.copyProperties(list.get(i), bomPartNew2);
                        bomPartNew2.setSeqNo(i + 1);
                        return bomPartNew2;
                    })
                    .collect(Collectors.toList());

            largeCargoUsageMapper.delDevelopBomUsage(request.getStyleNo(), request.getStyleColor(), companyId);
            largeCargoUsageMapper.addSdBomPartNew2List(updatedList);
        } else {
            largeCargoUsageMapper.delPcDiffPart(companyId,request.getStyleNo(),request.getStyleColor());
            largeCargoUsageMapper.delDevelopBomUsage(request.getStyleNo(), request.getStyleColor(), companyId);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 修改型体基本码
     * @param request
     * @return
     */
    @Override
    public ResponseInfo updateBaseSize(StyleColorMaterialAddRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo()) || StringUtils.isEmpty(request.getStyleColor())) {
            return ResponseInfo.error("型体或型体颜色不能为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(request.getStyleNo(), request.getStyleColor(),companyId);
        if (headNew2 != null && "Y".equals(headNew2.getCfmFlag())) {
            return ResponseInfo.error("型体资料已检核!");
        }
        //型体基本号
        largeCargoUsageMapper.updateBaseSize(request.getBaseSize(),request.getStyleNo(),request.getStyleColor(),companyId);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 重排序号
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getReordering(StyleNoToColorRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo()) || StringUtils.isEmpty(request.getStyleColor())) {
            log.error("型体:{},颜色:{},不能为空!", request.getStyleNo(), request.getStyleColor());
            return ResponseInfo.error("型体或型体颜色不能为空!");
        }

        //重排序号
        String companyId = UserIdThread.get().getCompanyId();
        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(request.getStyleNo(), request.getStyleColor(),companyId);
        if (headNew2 != null && "Y".equals(headNew2.getCfmFlag())) {
            return ResponseInfo.error("型体资料已检核!");
        }
        List<SdBomPartNew2> updatedList = getSdBomPartNew2List(request.getStyleNo(), request.getStyleColor(),companyId);
        if (!ObjectUtils.isEmpty(updatedList)){
            largeCargoUsageMapper.delDevelopBomUsage(request.getStyleNo(), request.getStyleColor(), companyId);
            largeCargoUsageMapper.addSdBomPartNew2List(updatedList);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 删除打勾型体记录
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getDeleteTickRecords(StyleR2DDelRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo()) || StringUtils.isEmpty(request.getStyleColor())) {
            log.error("型体:{},颜色:{},不能为空!", request.getStyleNo(), request.getStyleColor());
            return ResponseInfo.error("型体或型体颜色不能为空!");
        }
        if (ObjectUtils.isEmpty(request.getSeqNoList())) {
            log.error("型体:{},颜色:{},选择记录为空!",
                    request.getStyleNo(),
                    request.getStyleColor(),
                    request.getSeqNoList());
            return ResponseInfo.error("请选择记录!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(request.getStyleNo(), request.getStyleColor(),companyId);
        if (headNew2 != null && "Y".equals(headNew2.getCfmFlag())) {
            return ResponseInfo.error("型体资料已检核!");
        }
        largeCargoUsageMapper.deleteTickRecords(request.getStyleNo(),
                request.getStyleColor(),
                companyId,
                request.getSeqNoList());

        //重排序号
        List<SdBomPartNew2> updatedList = getSdBomPartNew2List(request.getStyleNo(), request.getStyleColor(),companyId);
        if (!ObjectUtils.isEmpty(updatedList)){
            largeCargoUsageMapper.delDevelopBomUsage(request.getStyleNo(), request.getStyleColor(), companyId);
            largeCargoUsageMapper.addSdBomPartNew2List(updatedList);
        }
        return ResponseInfo.ok("操作成功!");
    }

    @Resource
    private PartServiceImpl partService;

    /**
     * 重排序号方法整合
     * @param styleNo
     * @param styleColor
     * @param companyId
     * @return
     */
    public List<SdBomPartNew2> getSdBomPartNew2List(String styleNo, String styleColor,String companyId) {
        List<SdBomPartNew2> list = largeCargoUsageMapper.getSdBomPartNew2List(styleNo, styleColor,companyId);
        if (!ObjectUtils.isEmpty(list)) {
            //重排序号
            //生成一个整数流
            List<SdBomPartNew2> updatedList = IntStream.range(0, list.size())
                    //映射对象
                    .mapToObj(i -> {
                        SdBomPartNew2 bomPartNew2 = new SdBomPartNew2();
                        BeanUtil.copyProperties(list.get(i), bomPartNew2);
                        //获取种类编码
                        String typeNo = partService.getTypeNo(list.get(i).getMtlNo2());
                        bomPartNew2.setMtlTypeNo(typeNo);
                        bomPartNew2.setSeqNo(i + 1);
                        return bomPartNew2;
                    })
                    .collect(Collectors.toList());
            return updatedList;
        }
        return null;
    }

    /**
     * 复制用量列表
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getCopyingDosageList(CopyingDosageQueryRequest request) {
        if (request == null || ObjectUtils.isEmpty(request.getCurrentStyleNo()) || ObjectUtils.isEmpty(request.getCurrentStyleColor())) {
            return ResponseInfo.error("请求参数为空!");
        }
        if (request.getPageNum() == null || request.getPageSize() == null) {
            request.setPageNum(1);
            request.setPageSize(10);
        }
        PageHelper.startPage(request.getPageNum(),request.getPageSize());
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<CopyingDosageListDto> copyingDosageList = largeCargoUsageMapper.getCopyingDosageList(request);

        PageRecord<CopyingDosageListDto> list = new PageRecord<>();
        PageInfo pageInfo = new PageInfo(copyingDosageList);
        list.setList(copyingDosageList);
        list.setTotal(pageInfo.getTotal());

        return ResponseInfo.ok(list);
    }

    /**
     * 复制用量
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getCopyingDosage(CopyingDosageRequest request) {
        if (ObjectUtils.isEmpty(request.getCurrentStyleNo()) || ObjectUtils.isEmpty(request.getCurrentStyleColor())) {
            return ResponseInfo.error("当前型体颜色为空!!");
        }
        if (ObjectUtils.isEmpty(request.getList())) {
            return ResponseInfo.error("请选择记录");
        }
        //记录检验条数
        int count = 0;
        for (StyleNoToColorRequest s : request.getList()) {
            if (!ObjectUtils.isEmpty(s) && !ObjectUtils.isEmpty(s.getCfmFlag())) {
                if (s.getCfmFlag().equals(DefineEnum.CHECKOUT_FLAG.getValue())) {
                    count++;
                }
            }
        }
        if (count > 0) {
            return ResponseInfo.error(count + "条记录已检核,不能处理!");
        }

        String companyId = UserIdThread.get().getCompanyId();
        //定义冲刀号计数
        int cutCount = 0;
        //获取当前型体的冲刀号
        List<String> currentSize = largeCargoUsageMapper.getTypeSize(request.getCurrentStyleNo(),companyId);
        if (!ObjectUtils.isEmpty(currentSize)) {
            for (StyleNoToColorRequest s : request.getList()) {
                //查询被复制的冲刀号
                List<String> copySize = largeCargoUsageMapper.getTypeSize(s.getStyleNo(), companyId);

                if (!ObjectUtils.isEmpty(copySize)) {
                    //判断元素是否存在
                    boolean exists = currentSize.stream().anyMatch(size -> copySize.contains(size));
                    if (!exists) {
                        cutCount++;
                    }
                }
            }
        }
        if (cutCount > 0) {
            return ResponseInfo.error(cutCount + "条记录冲刀号不一样,不能处理!");
        }

        // 查询当前用量明细资料
        List<YfBomPartNew2Dto> currentList = largeCargoUsageMapper.getDevelopBomUsageList(request.getCurrentStyleNo(), request.getCurrentStyleColor(), companyId);
        if (!ObjectUtils.isEmpty(currentList)) {
            // 将 copyList 转换为 Map 方便查找
            Map<String, YfBomPartNew2Dto> copyMap = new HashMap<>();
            for (StyleNoToColorRequest s : request.getList()) {
                List<YfBomPartNew2Dto> copyList = largeCargoUsageMapper.getDevelopBomUsageList(s.getStyleNo(), s.getStyleColor(), companyId);
                if (!ObjectUtils.isEmpty(copyList)) {
                    copyList.forEach(copy -> copyMap.put(copy.getPartName(), copy));
                }
            }

            // 更新操作移动到循环外部
            List<YfBomPartNew2Dto> updatedList = new ArrayList<>();
            currentList.forEach(current -> {
                YfBomPartNew2Dto copy = copyMap.get(current.getPartName());
                if (copy != null && current.getSegment().equals(copy.getSegment())&& current.getMtlNo().equals(copy.getMtlNo())) {
                    copy.setUsage(current.getUsage());
                    copy.setUsageType(current.getUsageType());
                    copy.setDrawDept(current.getDrawDept());
                    updatedList.add(copy);
                }
            });

            // 批量更新数据库
            if (!ObjectUtils.isEmpty(updatedList)) {
                largeCargoUsageMapper.updateBomCopyingDosage(updatedList);
            }
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * bom资料删除
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getDeleteBomStyleColor(StyleNoToColorRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo()) || StringUtils.isEmpty(request.getStyleColor())) {
            log.error("型体:{},颜色:{},不能为空!", request.getStyleNo(), request.getStyleColor());
            return ResponseInfo.error("型体或型体颜色不能为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(request.getStyleNo(), request.getStyleColor(),companyId);
        if (headNew2 != null && "Y".equals(headNew2.getCfmFlag())) {
            return ResponseInfo.error("型体BOM资料已检核,不能删除!");
        }
        largeCargoUsageMapper.delBomHead(request.getStyleNo(),request.getStyleColor(), companyId);
        largeCargoUsageMapper.delNotesForFiling(request.getStyleNo(),request.getStyleColor(), companyId);
        largeCargoUsageMapper.delDevelopBomUsage(request.getStyleNo(), request.getStyleColor(), companyId);
        largeCargoUsageMapper.delBomUsage(request.getStyleNo(), request.getStyleColor(), companyId);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 级放部位全码用量
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getPartUsage(SizeUsageRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo()) || StringUtils.isEmpty(request.getStyleColor())) {
            return ResponseInfo.error("型体或型体颜色不能为空!");
        }
        if (ObjectUtils.isEmpty(request.getUsage())){
            return ResponseInfo.error("该部位用量未定义!");
        }
        if (ObjectUtils.isEmpty(request.getUsageType())){
            return ResponseInfo.error("该部位的计算方式不是中间码/全码!");
        }
        if (!ObjectUtils.isEmpty(request.getUsageType())&&!request.getUsageType().equals(DefineEnum.MIDDLE_CODE.getValue())&&!request.getUsageType().equals(DefineEnum.FULL_CODE.getValue())){
            return ResponseInfo.error("该部位的计算方式不是中间码/全码!");
        }
        if (ObjectUtils.isEmpty(request.getBaseSize())){
            return ResponseInfo.error("基本码未定义!");
        }
        String baseSize = request.getBaseSize();
        Map<String, BigDecimal> map1 = getSizeC();
        BigDecimal sizeNo1 = map1.get(baseSize);
        if (!ObjectUtils.isEmpty(sizeNo1)) {
            request.setBaseSize(String.valueOf(sizeNo1));
        }

        //查询鞋码
        String companyId = UserIdThread.get().getCompanyId();
        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(request.getStyleNo(), request.getStyleColor(),companyId);
        if (headNew2 != null && "Y".equals(headNew2.getCfmFlag())) {
            return ResponseInfo.error("型体资料已检核!");
        }
        List<YfStyleSizeDto> sizeList = largeCargoTypeMapper.getSellStyleSizeList(request.getStyleNo(),companyId);
        List<YfStyleSizeDto> exist = sizeList.stream()
                .filter(dto -> dto.getSizeNo().equals(request.getBaseSize()))
                .collect(Collectors.toList());
        if (exist.size() == 0){
            return ResponseInfo.error("型体配码资料无此号码!");
        }

        if (!ObjectUtils.isEmpty(sizeList)){

            //童鞋处理
            List<Map<String, BigDecimal>> list = sizeList.stream()
                    .filter(s -> s.getSizeNo() != null && !s.getSizeNo().isEmpty())
                    .map(s -> {
                        Map<String, BigDecimal> map = getSizeC();
                        BigDecimal sizeNo = map.get(s.getSizeNo());
                        if (!ObjectUtils.isEmpty(sizeNo)) {
                            s.setSizeNo(String.valueOf(sizeNo));
                        }
                        map.put(s.getSizeNo(),BigDecimal.valueOf(Double.valueOf(request.getBaseSize())));
                        return map;
                    }).collect(Collectors.toList());

            if (!ObjectUtils.isEmpty(list)){
                List<SdBomUsageNew2> bomUsageNew2List = new ArrayList<>();
                for (Map<String, BigDecimal> map : list) {
                    for (String key : map.keySet()) {
                        SdBomUsageNew2 sdBomUsageNew2 = new SdBomUsageNew2();
                        BeanUtil.copyProperties(request,sdBomUsageNew2);
                        sdBomUsageNew2.setSizeFrom(key);
                        sdBomUsageNew2.setCompanyId(companyId);
                        sdBomUsageNew2.setUsageType(null);
                        BigDecimal value = map.get(key);
                        int result = value.compareTo(BigDecimal.valueOf(Double.valueOf(request.getBaseSize())));
                        if (result < 0) {
                            //相差的号码数
                            BigDecimal sum = BigDecimal.valueOf(Double.valueOf(request.getBaseSize())).subtract(value);
                            //要减去的值
                            BigDecimal size = sum.multiply(request.getUsage().multiply(new BigDecimal(0.05)));
                            BigDecimal usage = request.getUsage().subtract(size);
                            sdBomUsageNew2.setMtlUsage(usage.setScale(5, RoundingMode.HALF_UP));
                        } else if (result > 0) {
                            //相差的号码数
                            BigDecimal sum = value.subtract(BigDecimal.valueOf(Double.valueOf(request.getBaseSize())));
                            //要加上去的值
                            BigDecimal size = sum.multiply(request.getUsage().multiply(new BigDecimal(0.05)));
                            BigDecimal usage = request.getUsage().add(size);
                            sdBomUsageNew2.setMtlUsage(usage.setScale(5, RoundingMode.HALF_UP));
                        } else {
                            sdBomUsageNew2.setMtlUsage(request.getUsage());
                        }
                        bomUsageNew2List.add(sdBomUsageNew2);
                    }
                }
                largeCargoUsageMapper.delBomUsage(request.getStyleNo(),request.getStyleColor(), companyId);
                largeCargoUsageMapper.addSdBomUsageNew2List(bomUsageNew2List);
            }
        }
        largeCargoUsageMapper.updateBaseSize(baseSize,request.getStyleNo(),request.getStyleColor(), companyId);
        return ResponseInfo.ok("操作成功!");
    }

    public Map<String, BigDecimal>  getSizeC(){
        Map<String, BigDecimal> map = new HashMap<>();
        map.put("C13.5", BigDecimal.valueOf(0.5));
        map.put("C13", BigDecimal.valueOf(0));
        map.put("C12.5", BigDecimal.valueOf(-0.5));
        map.put("C12", BigDecimal.valueOf(-1));
        map.put("C11.5", BigDecimal.valueOf(-1.5));
        map.put("C11", BigDecimal.valueOf(-2));
        map.put("C10.5", BigDecimal.valueOf(-2.5));
        map.put("C10", BigDecimal.valueOf(-3));
        return map;
    }

    /**
     * 全码用量资料查询
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getFullCodeSizeList(FullCodeRenewGetRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo()) || StringUtils.isEmpty(request.getStyleColor())) {
            return ResponseInfo.error("型体或型体颜色不能为空!");
        }
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<SdBomUsageNew2> fullCodeSizeList = largeCargoUsageMapper.getFullCodeSizeList(request);
        List<Map<String,String>> list=new ArrayList<>();
        for (SdBomUsageNew2 sdBomUsageNew2 : fullCodeSizeList) {
            Map<String,String> map=new HashMap<>();
            map.put("sizeFrom",sdBomUsageNew2.getSizeFrom());
            map.put("mtlUsage", String.valueOf(sdBomUsageNew2.getMtlUsage()));
            list.add(map);
        }
        return ResponseInfo.ok(list);
    }

    /**
     * 删除全码用量资料
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getFullCodeSizeDel(FullCodeRenewRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo()) || StringUtils.isEmpty(request.getStyleColor())) {
            return ResponseInfo.error("型体或型体颜色不能为空!");
        }
        if (ObjectUtils.isEmpty(request.getList())) {
            log.error("删除全码用量资料参数为空!");
            return ResponseInfo.error("请选择全码用量资料!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        largeCargoUsageMapper.delFullCodeSize(request.getStyleNo(), request.getStyleColor(), request.getList(),companyId);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 全码用量资料修改
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getFullCodeDetailRenewA2U(FullCodeRenewRequest request) {
        if (StringUtils.isEmpty(request.getStyleNo()) || StringUtils.isEmpty(request.getStyleColor())) {
            return ResponseInfo.error("型体或型体颜色不能为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(request.getStyleNo(), request.getStyleColor(),companyId);
        if (headNew2 != null && "Y".equals(headNew2.getCfmFlag())) {
            return ResponseInfo.error("型体资料已检核!");
        }
        if (!ObjectUtils.isEmpty(request.getList())){
            List<SdBomUsageNew2> list = request.getList().stream().map(s -> {
                SdBomUsageNew2 sdBomUsageNew2 = new SdBomUsageNew2();
                sdBomUsageNew2.setCompanyId(companyId);
                sdBomUsageNew2.setStyleNo(request.getStyleNo());
                sdBomUsageNew2.setStyleColor(request.getStyleColor());
                BeanUtil.copyProperties(s, sdBomUsageNew2);
                return sdBomUsageNew2;
            }).collect(Collectors.toList());
            largeCargoUsageMapper.delBomUsage(request.getStyleNo(),request.getStyleColor(), companyId);
            largeCargoUsageMapper.addSdBomUsageNew2List(list);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 贴合子材料明细
     * @param map
     * @return
     */
    @Override
    public ResponseInfo getDetailsOfAdhesiveSubMaterial(Map<String, String> map) {
        String mtlNo = map.get("mtlNo");
        String companyId = UserIdThread.get().getCompanyId();
        List<YfSubMtlTemp2Dto> list = largeCargoUsageMapper.getDetailsOfAdhesiveSubMaterial(mtlNo,companyId);
        return ResponseInfo.ok(list);
    }

    /**
     * 贴合子材料-修改
     * @param map
     * @param choose
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getDetailsOfAdhesiveSubMaterialUpdate(Map<String, String> map, Integer choose) {
        String mtlNo = map.get("mtlNo");
        String mtlSeq = map.get("mtlSeq");
        String styleNo = map.get("styleNo");
        String styleColor = map.get("styleColor");
        String percent = map.get("percent");
        if (ObjectUtils.isEmpty(styleColor)||StringUtils.isEmpty(styleNo)){
            return ResponseInfo.error("型体或型体颜色为空!");
        }
        if (ObjectUtils.isEmpty(mtlNo) || ObjectUtils.isEmpty(mtlSeq)) {
            return ResponseInfo.error("物料编码或序号为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        String userName = UserIdThread.get().getUserName();
        SdBomHeadNew2 headNew2 = largeCargoUsageMapper.sdBomHeadNew2(styleNo,styleColor,companyId);
        if (ObjectUtils.isEmpty(headNew2)){
            return ResponseInfo.error("型体资料不存在!");
        }
        if ("Y".equals(headNew2.getCfmFlag())) {
            return ResponseInfo.error("型体资料已检核!");
        }

        //0请购,1不请购
        switch (choose){
            case 0:
                largeCargoUsageMapper.UpdateDetailsOfAdhesiveSubMaterial(mtlNo,mtlSeq,DefineEnum.CHECKOUT_FLAG.getValue(),companyId,percent,userName);
                YfBomFitSubLog subLog = addSdBomFitSub(mtlNo, mtlSeq, companyId);
                subLog.setPurFlag(DefineEnum.CHECKOUT_FLAG.getValue());
                subLog.setRemark(DefineEnum.PURCHASE_REQUEST.getValue());
                largeCargoUsageMapper.addSdBomFitSubLog(subLog);
                break;
            default:
                largeCargoUsageMapper.UpdateDetailsOfAdhesiveSubMaterial(mtlNo,mtlSeq,null,companyId,percent,userName);
                YfBomFitSubLog subLog2 = addSdBomFitSub(mtlNo, mtlSeq, companyId);
                subLog2.setPurFlag(null);
                subLog2.setRemark(DefineEnum.NO_PURCHASE_REQUEST.getValue());
                largeCargoUsageMapper.addSdBomFitSubLog(subLog2);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 添加子表记录
     * @param mtlNo
     * @param mtlSeq
     * @param companyId
     * @return
     */
    public YfBomFitSubLog addSdBomFitSub(String mtlNo, String mtlSeq,String companyId){
        SdBomFitSub material = largeCargoUsageMapper.getSdBomFitSub2(mtlNo, mtlSeq,companyId);
        YfBomFitSubLog subLog = new YfBomFitSubLog();
        BeanUtil.copyProperties(material, subLog);
        subLog.setSysDate(LocalDateTime.now());
        subLog.setSysUser(UserIdThread.get().getUserName());
        return subLog;
    }

    /**
     * bom导出
     * @param request
     * @param response
     */
    @Override
    public void bomExport(UsageDetailsQueryChangeRequest request, HttpServletResponse response) {
        String companyId = UserIdThread.get().getCompanyId();
        request.setCompanyId(companyId);
        List<StyleToPhotoDto> photoList = largeCargoUsageMapper.getDevelopBomPhotoList(request);
        if (!ObjectUtils.isEmpty(photoList)){
            StyleToPhotoDto styleToPhotoDto = photoList.get(0);
            Map<String, Object> map = new HashMap<>();
            map.put("styleNo",request.getStyleNo());
            map.put("styleColor",request.getStyleColor());
            String photoPath = styleToPhotoDto.getPhotoPath();
            SdStyleType sdStyleType = largeCargoTypeMapper.getSdStyleTypeDetail(request.getStyleNo(), companyId);
            if (!ObjectUtils.isEmpty(sdStyleType)){
                map.put("lastMold",sdStyleType.getLastMold());
                map.put("solePart",sdStyleType.getSolePart());
                map.put("midsole",sdStyleType.getMidsole());
            }
            List<SdStyleSize> numberList = largeCargoTypeMapper.getSdStyleSizeDetail(request.getStyleNo(), companyId);
            map.put("numberList",numberList);
            List<YfBomMemoNew2Dto> noteList = largeCargoUsageMapper.getNotesForFilingList(request.getStyleNo(),request.getStyleColor(), companyId);
            map.put("noteList",noteList);
            String userName = UserIdThread.get().getUserName();
            LocalDate date = LocalDate.now();
            map.put("userName",userName);
            map.put("date",date);
            List<SdBomPartNew2> list = largeCargoUsageMapper.getSdBomPartNew2List(request.getStyleNo(), request.getStyleColor(), companyId);
            for (SdBomPartNew2 t : list) {
                if (!ObjectUtils.isEmpty(t.getUsageType())) {
                    switch (t.getUsageType()) {
                        case "B":
                            t.setUsageType(DefineEnum.BASIC_CODE.getValue());
                            break;
                        case "M":
                            t.setUsageType(DefineEnum.MIDDLE_CODE.getValue());
                            break;
                        case "F":
                            t.setUsageType(DefineEnum.FULL_CODE.getValue());
                            break;
                        default:
                            t.setUsageType(null);
                    }
                }
            }
            map.put("list",list);
            map.put("sheets",Arrays.asList(0));
            String fileName = request.getStyleNo() + "_"+request.getStyleColor()+"_" + DateUtils.dateTimeNow() + ".xls";
            TemplateExportParams templateParams = new TemplateExportParams("templates/大货用量BOM导出模板.xlsx", true);
            templateParams.setColForEach(true);
            try {
                byte[] imageBytes = ImageUtils.readAndResizeImage(photoPath, 220, 130);
                ExcelUtils.setResponseZF(response, fileName);
                ExcelPoiUtils.writeExcelWithPictureToResponse(response, templateParams, map, imageBytes, 25, 7, 2, 3);
            } catch (Exception e) {
                log.error(request.getStyleNo() + "_"+request.getStyleColor()+"表导出失败, 错误信息: {}", e.getMessage());
                throw new ServiceErrorException("大货型体BOM导出失败!");
            }
        }
    }

}
